package com.meitianhui.convenitenbuy.module.wallet.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletCouponContract;
import com.meitianhui.convenitenbuy.module.wallet.http.WalletApiDataSource;
import com.meitianhui.convenitenbuy.module.wallet.model.CouponBean;
import com.meitianhui.convenitenbuy.module.wallet.ui.activity.WalletCouponActivity;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/2.
 */

public class WalletCouponPresenter implements WalletCouponContract.Presenter {

    private WalletCouponActivity context;
    private final WalletCouponContract.View view;
    private Gson gson;

    public WalletCouponPresenter(WalletCouponActivity view,
                                 WalletCouponActivity context) {
        this.view = view;
        this.context = context;
        gson = new Gson();
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getCoupons() {
        UserToken bean = InfoProvider.getUserToken(App.getInstance());
        String userToken = bean.getUser_token();
        String service = "finance.memberCouponFind";

        Map<String, String> map = new HashMap<>();
        map.put("member_id", bean.getMember_id());
        String jsonParams = new Gson().toJson(map);

        WalletApiDataSource.getInstance()
                .getCoupons(userToken, service, jsonParams)
                .subscribe(new ResponseObserver<BaseResponse<CouponBean>>(context, true) {
                    @Override
                    public void onSuccess(BaseResponse<CouponBean> response) {
                        view.getCouponBack(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.getCouponBackFail(msg);
                    }
                });
    }

}
